package com.example.service.shop;

import com.example.entity.shop.DamageReport;
import com.example.entity.shop.SellerProduct;
import com.example.mapper.DamageReportMapper;
import com.example.mapper.SellerProductMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title: DamageReportService
 * @Author 潘春兰
 * @Package com.example.service.shop
 * @Date 2025/2/11 22:14
 * @description:
 * @Version:
 */
@Service
public class DamageReportService {
    @Resource
    private DamageReportMapper damageReportMapper;
    @Resource
    private SellerProductMapper sellerProductMapper;
@Transactional
    public void insert(DamageReport damageReport) {
        SellerProduct sellerProduct = sellerProductMapper.selectById(damageReport.getProductId());
        if(sellerProduct.getCurrentStock()<damageReport.getQuantity()){
            damageReport.setReportCause("缺货");
        }
        //将报损的数量从库中减去
        sellerProduct.setCurrentStock(sellerProduct.getCurrentStock()-damageReport.getQuantity());
        sellerProductMapper.updateById(sellerProduct);
        damageReportMapper.insert(damageReport);
    }

    public PageInfo<DamageReport> selectAssociation(DamageReport damageReport, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<DamageReport> list = damageReportMapper.selectAssociation(damageReport);
        return PageInfo.of(list);

    }

    /**
     * 根据id删除报损信息
     * @param id
     */
    public void deleteById(Integer id) {
        damageReportMapper.deleteById(id);
    }

    /**
     * 用于修改删除状态
     * @param damageReport
     */
    public void updateById(DamageReport damageReport) {
        damageReport.setDeletedStatus("1");
        damageReportMapper.updateById(damageReport);
    }

    public void updateByIdBatch(List<DamageReport> damageReports)
    {
        for (DamageReport damageReport : damageReports) {
            damageReportMapper.updateById(damageReport);
        }
    }
    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids)
            {
                for (Integer id : ids) {
                    damageReportMapper.deleteById(id);
                }
            }

    /**
     * 根据商品名称模糊查询报损信息
     * @param sellerProduct
     * @param pageNum
     * @param pageSize
     * @return
     */

    public PageInfo<DamageReport> selectByName(SellerProduct sellerProduct, Integer pageNum, Integer pageSize) {
                //根据商品名称模糊查询商品信息
                List<SellerProduct> sellerProductList = sellerProductMapper.selectAll(sellerProduct);
                //从商品中获取商品的id
                List<Integer> productIds = sellerProductList.stream()
                        .map(SellerProduct::getSellerProductId)
                        .collect(Collectors.toList());
                if (productIds.isEmpty()) {
                    return PageInfo.emptyPageInfo();
                }
                //根据id从报损表中查询报损信息
                PageHelper.startPage(pageNum, pageSize);
               List<DamageReport> damageReportList = damageReportMapper.selectByIds(productIds);
                //把所有的报损信息返回给客户端
                return PageInfo.of(damageReportList);
            }

}
